import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/model/commodity_details_entity.dart';
import 'package:flutter_ncsjqz_getx/page/farm/sign/sign_view.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/gradient_button.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/row_line_widget.dart';
import 'package:get/get.dart';

import 'order_logic.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/12/28 13:48
/// filePath： lib/page/farm/order/order_view.dart
/// desc: 确认订单
/// <summary>
///
class OrderPage extends StatefulWidget {
  // 商品信息
  CommodityDetailsData? commodityDetailData;
  // 商品sku信息
  CommodityDetailsDataSkuList? commodityDetailsDataSkuList;

  OrderPage({this.commodityDetailData, this.commodityDetailsDataSkuList});

  @override
  _OrderPageState createState() => _OrderPageState();
}

class _OrderPageState extends State<OrderPage> {
  final logic = Get.put(OrderLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.wxEvent();
    logic.commodityDetailData = widget.commodityDetailData;
    logic.commodityDetailsDataSkuList = widget.commodityDetailsDataSkuList;
  }

  getSkuString(){
    String skuStr='';
    // 组装我点击的sku
    widget.commodityDetailData!.props!.forEach((element) {
      element.propValues!.forEach((element2) {
        if(element2.select==null || element2.select==false){
          widget.commodityDetailsDataSkuList = CommodityDetailsDataSkuList();
        }else{
          int indexWhere =element.propValues!.indexWhere((element2) => element2.select == null ? false : element2.select! );
          CommodityDetailsDataPropsPropValues shopDataContentPropsPropsPropValue = element.propValues![indexWhere];
          skuStr+=element.propName! +':'+shopDataContentPropsPropsPropValue.propValue!+';';
        }
      });
    });
    if(skuStr.lastIndexOf(';') == skuStr.length-1){//有最后一个分号 需要去掉
      skuStr=skuStr.substring(0,skuStr.lastIndexOf(';'));
    }
    return skuStr;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBarWidget(title: "确认订单"),
      body: Column(
        children: [
          buildShopInfo(),
          Divider(color: Colors.grey.shade200,height: 1,),
          buildCount(),
          // Divider(color: Colors.grey.shade200,height: 1,),
          // buildLink(),
          Divider(color: Colors.grey.shade200,height: 1,),
          buildRemark(),
        ],
      ),
      bottomNavigationBar: Container(
        width: Get.width,
        height: 70,
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                InkWidget.title("总价：",color: Colors.green),
                InkWidget.title("￥${widget.commodityDetailsDataSkuList!.oriPrice! * widget.commodityDetailData!.amount!}",fonSize: 18,color: Colors.green),
              ],
            ),
            SizedBox(
              child: gradientButton("结算",circular: 35,onTap: (){
                logic.focusNode.unfocus();
                logic.submitOrder().then((value){
                  if(value!=null){
                    Get.to(()=>SignPage(skuId: widget.commodityDetailsDataSkuList!.skuId,orderId: value.orderId,))!.then((value){
                      if(value!=null){ // 返回签名文件地址
                        logic.signPath = value;
                        bottomMoneyPay();
                      }
                    });
                  }
                });
              }),
              width: 110,
              height: 40,
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<OrderLogic>();
    if(logic.streamSubscription!=null){
      logic.streamSubscription!.cancel();
    }
    super.dispose();
  }

  buildShopInfo() {
    return Container(
      padding: EdgeInsets.all(15),
      color: Colors.white,
      child: Row(
        children: [
          InkWidget.imgNet("${widget.commodityDetailData!.masterPic}",width: 80,height: 80,borderRadius: BorderRadius.circular(5)),
          SizedBox(width: 15,),
          SizedBox(
            width: Get.width - 80 - 15 - 30,
            height: 80,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(child: InkWidget.title("${widget.commodityDetailData!.commodityName}",maxLines: 2,),),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(child: InkWidget.text("${widget.commodityDetailsDataSkuList!.properties}",color: Colors.grey,),),
                    InkWidget.text("￥${widget.commodityDetailsDataSkuList!.oriPrice}",color: Colors.grey.shade600),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  buildLink() {
    return Container(
      child: Row(
        children: [
          InkWidget.text("土地监控链接: ",color: Colors.grey,),
          InkWidget.text("https://www.baidu.com",color: Colors.green,onTap: (){

          }),
        ],
      ),
      color: Colors.white,
      padding: EdgeInsets.all(15),
    );
  }

  buildCount() {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          InkWidget.text("购买数量",color: Colors.grey,),
          InkWidget.text("${widget.commodityDetailData!.amount}",color: Colors.green),
        ],
      ),
      color: Colors.white,
      padding: EdgeInsets.all(15),
    );
  }

  buildRemark() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 15),
      color: Colors.white,
      child: TextField(
        focusNode: logic.focusNode,
        maxLines: 10,
        controller: logic.textEditingController,
        keyboardType: TextInputType.multiline,
        decoration: InputDecoration(
          hintText: "请输入备注信息",
          hintStyle: TextStyle(color: Colors.grey),
          border: InputBorder.none,
        ),
      ),
    );
  }

  bottomMoneyPay(){
    Get.bottomSheet(
      GetBuilder<OrderLogic>(
        builder: (controller) {
          List payType = ["微信支付","支付宝支付"];
          List payIcon = ["wx.png","zfb.png","zfb.png"];
          double height = payType.length < 3 ?  280 : 345;
          return SingleChildScrollView(
            physics: NeverScrollableScrollPhysics(),
            child: Material(
              child: Container(
                height: height,
                color: Colors.white,
                padding: EdgeInsets.all(15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Center(
                      child: SizedBox(
                        height: 60,
                        child:InkWidget.title("\n￥${widget.commodityDetailsDataSkuList!.oriPrice! * widget.commodityDetailData!.amount!}",fonSize: 25,color: Colors.black),
                      ),
                    ),
                    InkWidget.title("\n选择支付方式"),
                    /// todo：支付方式
                    Expanded(
                      child: ListView(
                        shrinkWrap: true,
                        children: List.generate(payType.length, (index){
                          return rowBaseWidget(
                              left: Row(
                                children: [
                                  InkWidget.imgAsset("${payIcon[index]}",width: 22,fit: BoxFit.fill),
                                  InkWidget.text(" ${payType[index]}"),
                                ],
                              ),
                              height: 45,
                              right:  InkWidget.imgAsset("select.png",width: 20,color: logic.payType != index ? Colors.white:null),
                              padding: EdgeInsets.all(0),
                              onTap: (){
                                logic.setPayType(index);
                              }
                          );
                        }),
                      ),
                    ),
                    Center(
                      child: SizedBox(
                        width: Get.width * 0.7,
                        child: gradientButton("确 定",onTap: (){
                          logic.orderPay();
                        },circular: 40),
                      ),
                    ),
                  ],
                ),
              ),
              color: Colors.transparent,
            ),
          );
        },
      ),
      enableDrag: false,
      isScrollControlled: true,
    ).then((value) {
      logic.setPayType(0);
    });
  }
}
